<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" typy="text"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>

      <el-form-item prop="email" label="邮箱">
        <el-input v-model="ruleForm.email" type="email"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="success" @click="submitForm">添加</el-button>
        <el-button @click="resetForm" type="primary">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="50" label="编号"> </el-table-column>
      <el-table-column prop="username" label="姓名" width="150">
      </el-table-column>
      <el-table-column prop="password" label="密码" width="150">
      </el-table-column>
      <el-table-column prop="email" label="邮编" width="200"> </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="deleteRow(scope.$index, tableData)"
            type="primary"
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <button @click="jumpTwo" class="jumpTwo">跳转到作业二</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        password: "",
        username: "",
        email: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 12, message: "账号长度3-12位", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 6, message: "密码长度3-6位", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur"],
          },
        ],
      },
      tableData: [
        {
          username: "王小虎",
          password: "123",
          email: "1741371660@qq.com",
        },
      ],
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((a) => {
        if (a) {
          this.tableData.push(this.ruleForm);
        } else {
          this.$message({
            message: "请检查您输入的内容是否有误",
            type: "warning",
          });
        }
      });
    },
    resetForm() {
      this.ruleForm = {
        password: "",
        username: "",
        email: "",
      };
    },

    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    jumpTwo() {
      this.$router.push("/worktwo");
    },
  },
};
</script>

<style lang="sass" scoped>
.el-button
    width: 200px
.el-input
    width: 500px
.el-table__body
    .el-button
        width: 50px
.jumpTwo
    margin-top: 50px
</style>